package ucm;

import java.sql.Connection;
import java.sql.Statement;
import model.Database_Model;
import model.User_Model;

public class UCRegisterUser {

    private String name, psw, rpsw, email;
    private User_Model user;

    public UCRegisterUser() {
    }

    public UCRegisterUser(view.Air_Register reg) {
        this.user = reg.getUserModel();

        this.name = user.getUsername();
        this.psw = user.getPassword();
        this.rpsw = user.getRPsw();
        this.email = user.getEmail();
    }

    public String run() {
        boolean inserted = false;
        if (this.name.isEmpty()) {
            return "Please input your name in the list.";
        } else if (this.psw.isEmpty()) {
            return "Please input your password in the list.";
        } else if (name.length() > 10 || psw.length() > 15) {
            return "Please input user name or password by correct form.";
        } else if (this.rpsw.isEmpty()) {
            return "Please input your password twice in the list.";
        } else if (!this.psw.equalsIgnoreCase(this.rpsw)) {
            return "Sorry, the two passwords you entered are different.";
        } else {
            String user_name = new ucm.UCCheckUser(name).run();
            if (user_name.equalsIgnoreCase("The user name already exists.")) {
                return "The user name already exists.";
            } else {
                try {
                    Connection con = new Database_Model().getConnection();
                    Statement stmt = con.createStatement();
                    stmt.executeUpdate("insert into user value ('" + name + "','" + psw + "','" + email + "','1')");
                    return "Congratulate! ";
                }catch(Exception e){
                    return "Failed! ";
                }
            }
        }
    }
}